val s=Staging
import Random._
def sierpinskiFirst(p:Point,side:Int):Unit= {
    axesOn()
    clear()
    setPenThickness(1)
    turtle0.invisible()
    var p1=new Point(p.x+side,p.y)
    var p2=new Point(p.x+(side/2).toDouble,p.y+(side*Math.sqrt(3)/2))
    s.line(p,p1)
    s.line(p2,p1)
    s.line(p,p2)
    def sierpinskiFirst1(po:Point,side:Int):Unit= {
        if(side<=3) return 
        else {
            var po1=new Point(po.x+side,po.y)
            var po2=new Point(po.x+(side/2).toDouble,po.y+(side*Math.sqrt(3)/2))
            var mid1=new Point((po.x+po1.x)/2,(po.y+po1.y)/2)
            var mid2=new Point((po.x+po2.x)/2,(po.y+po2.y)/2)
            var mid3=new Point((po2.x+po1.x)/2,(po2.y+po1.y)/2)
            s.line(mid1,mid2)
            s.line(mid3,mid2)
            s.line(mid1,mid3)
            sierpinskiFirst1(po,side/2)
            sierpinskiFirst1(mid1,side/2)
            sierpinskiFirst1(mid2,side/2)
        }
    }
    return sierpinskiFirst1(p,side)
}

sierpinskiFirst(new Point(-160,160),150)

def sierpinskiSec(noIt:Int,p:Point,start:Point,side:Int):Unit= {
    axesOn()
    setPenThickness(1)
    turtle0.invisible()
    var p1=new Point(p.x+side,p.y)
    var p2=new Point(p.x+(side/2).toDouble,p.y+(side*Math.sqrt(3)/2))
    s.line(p,p1)
    s.line(p2,p1)
    s.line(p,p2)
    def sierpinskiSec1(noIt:Int,start:Point):Unit= {
        if(noIt==0) return
        else {
            var p1=new Point(p.x+side,p.y)
            var p2=new Point(p.x+(side/2).toDouble,p.y+(side*Math.sqrt(3)/2))
            s.line(p,p1)
            s.line(p2,p1)
            s.line(p,p2)
            var pt=new Point(0,0)
            var x1=Random.nextInt(3)
            if(x1==0) {
                pt=p
            }
            else if(x1==1) {
                pt=p1
            }
            else {
                pt=p2
            }
            var newArg=new Point((start.x+pt.x)/2,(start.y+pt.y)/2)
            s.dot(newArg)
            return sierpinskiSec1(noIt-1,newArg)
      }
      
    }
    return sierpinskiSec1(noIt,start)
}
    
sierpinskiSec(5000,new Point(60,60),new Point(100,100),150)
